export default {
	install(Vue, options) {
		// let url = "https://client.sssmei.cn"; //图片地址CDN
		
		let url = uni.getStorageSync('serverUrl');
		
		let picUrl= 'https://client.sssmei.cn';
		
		let appid = "wx7d5280c4b7c4ac74"; // appid
		
		let urlPath = '/api/user/moreTableList'; //接口地址
		
		//使用Vue.prototype添加全局变量或者方法

		//--------------------------------------------------会员登录获取token
		Vue.prototype.login = function() {

			uni.showLoading({
				title: '登录中...'
			});

			var that = this;
            
			uni.login({
				provider: 'weixin',
				success: async function(loginRes) {
					
					console.log('登录中');

					let param = {};
					param.code = loginRes.code;
					param.appid = appid;
					// param.appid = 'wx4c0ea88251300dbd';
					param.parent_id = 66;
					param.shop_id = 1;
					//param=JSON.stringify(param)

					// -------查询用户表获取owenr_id
					
					let param1 = {};
					param1.db = 1; //0获取缓存数据  1 获取非缓存数据  默认0
					param1.dbNum = ''; //缓存编号   API默认以MD5(param1)为编号
					param1.table = 'mini';//主表名称
					param1.listType = 2; //2查询全部  3分页查询
					param1.order = ''; //表无自增ID时追加为默认排序 无需给个字符空值
					param1.linkfield = ''; //主表关联字段名称（为空则非关联多表查询）
					param1.where = {'appid':appid}; //主表查询条件 无需给个字符空值
					param1.table_list =[];//多表参数数组名称 无需给个字符空或空数组
					
					uni.setStorageSync('shop_id',1)
					console.log(param);

					let res = await that.$fly.post('/api/login/login',{'msg':JSON.stringify(param)})
					
					console.log(res,'@@');
					
					if (res.code == 200) {
						setTimeout(function() {
							uni.hideLoading();
						}, 100);
						
						uni.setStorageSync('userInfo',res);//缓存接口域名

						console.log('登录成功！');
						
						// const res1 = await that.$fly.post(urlPath, {
						// 	msg: JSON.stringify(param1)
						// });
						that.loadData(); //加载数据渲染页面
						// if(res1.code==200){
						// 	uni.setStorageSync('mini',res1.data[0]);//缓存接口域名
							
						// }
						// console.log(res1,'@@@@');

					} else {

						setTimeout(function() {
							uni.hideLoading();
						}, 100);

						uni.showModal({
							title: '提示',
							content: '微信登录失败，下拉刷新重新授权！',
							success: function(res) {
								if (res.confirm) {
									setTimeout(() => { //延时重新授权
										Vue.prototype.login();
									}, 5000)
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});

					}
				}
			});


		}


		//--------------------------------------------------前台js生成图片
		Vue.prototype.fileUrl = function(fileId = 0) {
			return  picUrl  + fileId
		}

		//--------------------------------------------------前台js获取当前时间10位时间戳
		Vue.prototype.timest = function() {
			var tmp = Date.parse(new Date()).toString();
			tmp = tmp.substr(0, 10);
			return tmp;
		}

		//--------------------------------------------------前台js根据当前时间生成订单号
		Vue.prototype.GetDateNow = function() {
			var vNow = new Date();
			let sNow = '';
			sNow += String(vNow.getFullYear());
			sNow += String(vNow.getMonth() + 1);
			sNow += String(vNow.getDate());
			sNow += String(vNow.getHours());
			sNow += String(vNow.getMinutes());
			sNow += String(vNow.getSeconds());
			// sNow += String(vNow.getMilliseconds());//毫秒
			return sNow;
		}

		//--------------------------------------------------时间戳转标准格式日期时间
		Vue.prototype.fmtDate = function(obj) {
			var now = new Date(obj * 1000);
			var year = now.getFullYear();
			// var month = now.getMonth() + 1;
			var month = (now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1);
			// var date = now.getDate();
			var date = (now.getDate() < 10 ? "0" + (now.getDate()) : now.getDate());
			// var hour = now.getHours();
			var hour = (now.getHours() < 10 ? "0" + (now.getHours()) : now.getHours());
			// var minute = now.getMinutes();
			var minute = (now.getMinutes() < 10 ? "0" + (now.getMinutes()) : now.getMinutes());
			//var second = now.getSeconds();
			var second = (now.getSeconds() < 10 ? "0" + (now.getSeconds()) : now.getSeconds());
			return year + "-" + month + "-" + date + "   " + hour + ":" + minute + ":" + second;
		}

		//--------------------------------------------------时间戳转日期
		Vue.prototype.fmtDate2 = function(obj) {
			var now = new Date(obj * 1000);
			
			var year = now.getFullYear();
			var month = now.getMonth() + 1;
			var date = now.getDate();
			var hour = now.getHours();
			var minute = now.getMinutes();
			var second = now.getSeconds();
			return year + "-" + month + "-" + date;

		}


		//-----------------------JS校验价格和金额的方法
		Vue.prototype.isPriceNumber = function(_keyword) {
			var _keyword = _keyword.replace(/\s/g, ""); //正则去掉字符串中所有空格
			if (_keyword == "0" || _keyword == "0." || _keyword == "0.0" || _keyword == "0.00") {
				_keyword = "0";
				return true;
			} else {
				var index = _keyword.indexOf("0");
				var length = _keyword.length;
				if (index == 0 && length > 1) {
					/*0开头的数字串*/
					var reg = /^[0]{1}[.]{1}[0-9]{1,2}$/;
					if (!reg.test(_keyword)) {
						return false;
					} else {
						return true;
					}
				} else {
					/*非0开头的数字*/
					var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/;
					if (!reg.test(_keyword)) {
						return false;
					} else {
						return true;
					}
				}
				return false;
			}
		}


	}
}
